package com.yuncheng.spcyApi.controller;

import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 审评查验-申请事项基本信息
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-申请事项基本信息库接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcySqsxBaisc")
public class SpcySqsxBaiscController extends BaseController<SpcySqsxBasic, ISpcySqsxBaiscService> {

    private static final Logger log = LoggerFactory.getLogger(SpcySqsxBaiscController.class);

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private MyExcelUtils myExcelUtils;

    /**
     * 分页列表查询
     *
     * @param spcySqsxBasic
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Log(title = "申请事项基本信息库-分页列表查询")
    @ApiOperation(value = "申请事项基本信息库-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(SpcySqsxBasic spcySqsxBasic,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<SpcySqsxBasic> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcySqsxBasic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFsxmc()),SpcySqsxBasic::getFsxmc,spcySqsxBasic.getFsxmc());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFywflcode()),SpcySqsxBasic::getFywflcode,spcySqsxBasic.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqymc()),SpcySqsxBasic::getFqymc,spcySqsxBasic.getFqymc());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFtyshxydm()),SpcySqsxBasic::getFtyshxydm,spcySqsxBasic.getFtyshxydm());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqyfddbrfzr()),SpcySqsxBasic::getFqyfddbrfzr,spcySqsxBasic.getFqyfddbrfzr());

        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqylxr()),SpcySqsxBasic::getFqylxr,spcySqsxBasic.getFqylxr());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFszdq()),SpcySqsxBasic::getFszdq,spcySqsxBasic.getFszdq());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFxxdz()),SpcySqsxBasic::getFxxdz,spcySqsxBasic.getFxxdz());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqyzszcdz()),SpcySqsxBasic::getFqyzszcdz,spcySqsxBasic.getFqyzszcdz());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFsqdbh()),SpcySqsxBasic::getFsqdbh,spcySqsxBasic.getFsqdbh());
        queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFcpmc()),SpcySqsxBasic::getFcpmc,spcySqsxBasic.getFcpmc());

        IPage<SpcySqsxBasic> pageList = spcySqsxBaiscService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 添加
     *
     * @param spcySqsxBasic
     * @return
     */
    @Log(title = "申请事项基本信息库-添加")
    @ApiOperation(value = "申请事项基本信息库-添加")
    @PostMapping(value = "/addBasic")
    public HttpResult<?> add(@RequestBody SpcySqsxBasic spcySqsxBasic) {

        spcySqsxBaiscService.save(spcySqsxBasic);

        String operContent = "新增信息：";

        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcySqsxBasic);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC,operContent,spcySqsxBasic.getId(), LogConstant.LOGS_SQSX_BASIC, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    /**
     * 编辑
     *
     * @return
     */
    @Log(title = "申请事项基本信息库-编辑")
    @ApiOperation(value = "申请事项基本信息库-编辑")
    @PutMapping(value = "/editBasic")
    public HttpResult<?> edit(@RequestBody HashMap<String, String> buffer) {
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("申请事项基本信息库id不能为空");
        }
        String basicId = buffer.get("id");
        SpcySqsxBasic oldBasic = spcySqsxBaiscService.getById(basicId);
        SpcySqsxBasic basicUpdate = spcySqsxBaiscService.getById(basicId);
        if (basicUpdate == null){
            return HttpResult.error("当前申请事项基本信息库数据不存在，请重试");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(basicUpdate, buffer);
        } catch (IllegalAccessException e) {
            log.error("申请事项基本信息库修改出错【IllegalAccessException】-{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("申请事项基本信息库修改出错【InvocationTargetException】-{}",e.getMessage());
        }
        spcySqsxBaiscService.updateById(basicUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldBasic);
        operContent += LogUtils.BeanModifyMsg(basicUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC,operContent,basicId,LogConstant.LOGS_SQSX_BASIC,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @Log(title = "申请事项基本信息-批量删除")
    @ApiOperation(value = "申请事项基本信息-批量删除")
    @DeleteMapping(value = "/deleteBatchBasic")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcySqsxBasic> sqsxBasicList = spcySqsxBaiscService.listByIds(idsList);
        if (CollectionUtils.isEmpty(sqsxBasicList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcySqsxBasic basic : sqsxBasicList){
            String id = basic.getId();
            spcySqsxBaiscService.removeById(id);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(basic);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_BASIC,operContent,id,LogConstant.LOGS_SQSX_BASIC,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Log(title = "申请事项基本信息-通过id查询")
    @ApiOperation(value = "申请事项基本信息-通过id查询")
    @GetMapping(value = "/queryBasicById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        SpcySqsxBasic spcySqsxBasic = spcySqsxBaiscService.getById(id);
        return HttpResult.ok(spcySqsxBasic);
    }

    /**
     * 导出申请事项基本信息库数据-表格excel
     */
    @Log(title = "导出申请事项基本信息库数据-表格excel")
    @ApiOperation(value = "导出申请事项基本信息库数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "申请事项基本信息id字符逗号分割",dataType = "String")
    })
    @GetMapping("/exportSqsxBasicData")
    public void exportSqsxBasicData(@RequestParam(value = "ids",defaultValue = "")String ids,
                                 SpcySqsxBasic spcySqsxBasic,
                                 HttpServletResponse response){
        try {
            String name = "exportSqsxBasicData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<SpcySqsxBasic> sqsxBasicList = spcySqsxBaiscService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(sqsxBasicList)){
                    for (SpcySqsxBasic e : sqsxBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<SpcySqsxBasic> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFsxmc()),SpcySqsxBasic::getFsxmc,spcySqsxBasic.getFsxmc());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFywflcode()),SpcySqsxBasic::getFywflcode,spcySqsxBasic.getFywflcode());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqymc()),SpcySqsxBasic::getFqymc,spcySqsxBasic.getFqymc());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFtyshxydm()),SpcySqsxBasic::getFtyshxydm,spcySqsxBasic.getFtyshxydm());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqyfddbrfzr()),SpcySqsxBasic::getFqyfddbrfzr,spcySqsxBasic.getFqyfddbrfzr());

                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqylxr()),SpcySqsxBasic::getFqylxr,spcySqsxBasic.getFqylxr());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFszdq()),SpcySqsxBasic::getFszdq,spcySqsxBasic.getFszdq());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFxxdz()),SpcySqsxBasic::getFxxdz,spcySqsxBasic.getFxxdz());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFqyzszcdz()),SpcySqsxBasic::getFqyzszcdz,spcySqsxBasic.getFqyzszcdz());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFsqdbh()),SpcySqsxBasic::getFsqdbh,spcySqsxBasic.getFsqdbh());
                queryWrapper.like(StringUtils.isNotBlank(spcySqsxBasic.getFcpmc()),SpcySqsxBasic::getFcpmc,spcySqsxBasic.getFcpmc());

                List<SpcySqsxBasic> sqsxBasicList = spcySqsxBaiscService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(sqsxBasicList)){
                    for (SpcySqsxBasic e : sqsxBasicList){
                        list.add(e);
                    }
                }
            }
            myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_SQSX_BASIC_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出申请事项基本信息库数据出错-{}",e.getMessage());
        }
    }

}
